Skip to content

[PR] fix(log-mask): scrub JSON-quoted credentials and Groq/Hugging Face keys - #5939

Merged
ranxianglei merged 2 commits into
ranxianglei:masterfrom
aftabkh4n:fix/redact-json-quoted-credentials
Nov 8, 2026
Merged

ranxianglei merged 2 commits into
ranxianglei:masterfrom
aftabkh4n:fix/redact-json-quoted-credentials

Conversation

@aftabkh4n

Copy link
Copy Markdown
Contributor

Problem

redactSecretsInText runs on every log line (src/logger.ts). Through redactForPublish, it also runs on the diagnostics blob that users paste into public GitHub issues (src/web/diagnostics.ts).

Its key=value rule requires : or = immediately after the key name. In the JSON/dict shape that upstream error bodies and config dumps use, a closing quote comes first, so the rule never matches. These all passed through verbatim on current master:

{"error":{"message":"invalid","api_key":"0123456789abcdef0123"}}
{"user":"alice","password":"hunter2hunter2"}
{'access_token': 'ya29.a0AfH6SMBx3kL9ZqWv'}
Invalid API Key: gsk_8fJ2kLmN4pQrS7tUvW9xYz1aBcDeFgHiJkLmNoPqRsTuVwXyZ012
auth failed for hf_AbCdEfGhIjKlMnOpQrStUvWxYz12345678

The existing JSON test case ("api_key":"sk-secret-value-12") only passes because its value starts with sk-, which the separate key-prefix rule catches. A JSON-quoted key whose value has no known prefix was never masked.

Groq (gsk_…) and Hugging Face (hf_…) keys are also not covered by any prefix rule, although both providers are on PUBLIC_HOST_SUFFIXES.

Change (src/log-mask.ts)

  • Allow an optional quote between the key name and the separator: \b["']?\s*[:=]. The captured prefix keeps the quote, so the output is "api_key":"<masked 20 chars>".
  • Mask gsk_ and hf_ keys the same way as sk-: (?<![A-Za-z0-9])(?:gsk|hf)_[A-Za-z0-9]{20,}.

Kept verbatim: usage-count keys ("prompt_tokens":1234, "tokens": 1234, "max_tokens":4096), short values ("token":"abc"), and identifiers such as hf_hub_download. A second pass over redacted text is still a no-op.

Tests

  • New test in tests/log-mask.test.ts: the leaking shapes above, the negatives, and idempotency.
  • I couldn't install the repo's dev dependencies in my environment, so I didn't run the full suite, tsc or lint. I verified the change by loading src/log-mask.ts directly with Node's built-in type stripping. The module imports only node:crypto and node:os. Results:
    • Every assertion of the existing redactSecretsInText test, including its negatives, passes unchanged.
    • The new assertions fail on master and pass with this change.
  • CI will be the first full run. Happy to adjust anything it flags.

CHANGELOG.md: entry added under [Unreleased] → Fixed.

redactSecretsInText runs on every log line and, through redactForPublish,
on the diagnostics blob that users paste into public GitHub issues. Its
key=value rule required ":" or "=" right after the key name, so the
JSON/dict shape that upstream error bodies and config dumps use -
"api_key": "...", "password": "...", 'access_token': '...' - passed
through verbatim unless the value happened to start with a known prefix
such as sk-. The existing JSON test case only passed for that reason.

Allow an optional quote between the key name and the separator, and mask
gsk_ (Groq) and hf_ (Hugging Face) keys like sk- ones; both providers are
on PUBLIC_HOST_SUFFIXES. Usage-count keys ("prompt_tokens", "tokens",
"max_tokens") and short values stay verbatim, and a second pass over
redacted text is still a no-op.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014TonJ1geSd97oAtSBZofUK
@ranxianglei ranxianglei changed the title fix(log-mask): scrub JSON-quoted credentials and Groq/Hugging Face keys [PR] fix(log-mask): scrub JSON-quoted credentials and Groq/Hugging Face keys Nov 8, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

Duplicate screening: not a duplicate. No open issue/PR covers this exact gap (JSON-quoted key separator + missing gsk_/hf_ prefixes). Nearest neighbors are all closed and different mechanisms — #4191 (non-HTTP URL userinfo), #4427 (diagnostics raw log lines / absolute paths), #3811 (private relay hosts), #1718 (the original sink-level scrubber this builds on). On the mirror, only this PR (#5942) targets the topic — no parallel fix branch to fold into. Base is fresh (head contains current master), mergeable: true.

What I verified (not taken on faith):

  • Fetched the PR head (907fcea6) and ran every assertion from both the existing #1718 test and the new test against the PR-head src/log-mask.ts via Node type-stripping → all pass.
  • Ran the 5 leaking shapes against master's log-mask.ts → 5/5 pass through verbatim, confirming the bug is real and the new test has teeth (genuine regression test, not a tautology).
  • Added 5 extra negative probes of my own (quoted prose after token, secret as a noun, json-path, etc.) → all stay verbatim, so the broadened separator rule doesn't start over-masking.
  • Mapped all consumers of redactSecretsInText: only three pure display sinks (src/logger.ts:347, src/agent/host-log.ts:36, src/web/diagnostics.ts via redactForPublish). None hash/dedup/cursor on the output bytes.
  • Confirmed groq.com and huggingface.co are both in PUBLIC_HOST_SUFFIXES (so the "both on the public-host list" comment is accurate).

Behavior change (old → new, intended): previously JSON/dict-shaped credentials whose value lacked a known prefix leaked through logs and the public-issue diagnostics blob; now masked. Two new key prefixes (gsk_, hf_) join the mask set. This is the fix, not drift.

Live CI state (just checked): still running — 30 checks, 0 failed, 9 in progress (windows-22 shards 1&2 and static-gates/ubuntu-24 already green). I am not claiming full-green yet.

Process note (non-blocking): no source issue is referenced — this is a self-discovered standalone fix, which is fine for an external contribution. Suggested labels for triage: p2 + a security/wiring scope. I did not apply them (not my PR) and did not touch the diff (quality is clean — no refactor needed). Merge stays human-only.


结构化八问(供人审):

  1. 是什么问题:日志/发布脱敏器 redactSecretsInText 的 key=value 规则要求键名后紧跟 :/=;而 JSON/dict 形状("api_key":"…")键名后先出现闭引号,于是值没有已知前缀(如 sk-)时凭据原样漏进日志、并经 redactForPublish 进入贴到公开 GitHub issue 的诊断包。另:Groq(gsk_…)、Hugging Face(hf_…)两种本就在公开主机表里的 key 没有任何前缀规则覆盖。
  2. 什么原因导致的:两条独立根因——①分隔符正则 \b\s*[:=] 没允许键名与分隔符之间的引号;②前缀白名单只有 sk|xai-、AIza、JWT,缺 gsk_/hf_。属「该拦的没拦」的疏漏,非表面现象。
  3. 影响范围与严重程度:触发面 = 上游错误体/配置 dump 里以 JSON/dict 形式带凭据且值无已知前缀的场景 + Groq/HF 用户。严重度 = 凭据泄露(进本地日志 + 经诊断包进公开 issue),但需特定形状才触发、非核心功能不可用 → p2(明显安全问题,绕行 = 别把含此类 key 的诊断包贴公开)。
  4. 怎么修复的:①分隔符正则改 \b["']?\s*[:=],允许键名后一个可选引号(捕获组保留引号,输出仍为 "api_key":"<masked N chars>");②新增 (?<![A-Za-z0-9])(?:gsk|hf)_[A-Za-z0-9]{20,},长度阈值 20 用来放过 hf_hub_download/gsk_short 这类标识符。纯文本变换,在既有 .replace 链内加两行,不动架构。
  5. 是否有回归问题:无。逐条核过既有行为:bearer/basic、sk/xai、AIza、JWT 四条前缀规则逐字节未动;无引号的 key=value/key: value 因 ["']? 匹配空串而行为不变;计数键(prompt_tokens/tokens/max_tokens)、短值("token":"abc")、散文(the token was expired)、标识符全部保持原文(我另加 5 个负例探针实测通过);二次幂等(再跑一遍为 no-op)已测。高危模式(fail-fast 变吞错、阈值/时序/输出格式漂移)均不涉及——新遮蔽值沿用既有 <masked N chars>/<masked key> 格式,已遮蔽值的输出字节完全一致。
  6. 是否破坏缓存:不涉及。redactSecretsInText 只喂三个纯展示 sink(logger 每行、host-log、diagnostics 发布包),不参与任何缓存键/去重键/增量游标;全仓 grep 确认无对脱敏输出做 hash/dedup 的消费方。
  7. 是否改了设计方向:不涉及。仍是同一 sink 级兜底脱敏机制,只扩宽识别的分隔符形状并补两个前缀,边界/数据流未转向。
  8. 是否引入了新的配置:不涉及。无新 env/config/默认值。

判定:外部贡献者质量达标——双向断言齐备(符合 gate 纪律)、diff 收敛(3 文件,无跑题/空白噪音)、注释讲 why、测试在 master 上确红/head 上确绿。无需对齐重构。待 CI 全绿即可合入(合并权在人)。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

CI update: now fully complete — 18 passed, 12 skipped (matrix cells gated off by design for this 3-file change), 0 failed. The last open condition in my review (full-green) is met.

Verdict unchanged and now unblocked: clean external contribution, no refactor needed → ready to merge (merge stays human-only). Nothing further from me unless you want the p2 + security/wiring labels applied.

@ranxianglei
ranxianglei merged commit fdb30f8 into ranxianglei:master Nov 8, 2026
23 checks passed
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1089-master.fdb30f8 (rolling master tag).

npm install -g billion-context@0.0.1089-master.fdb30f8  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants